package us.versus.them.hmap;

#if flash
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.display.SimpleButton;
#end

/**
 *
 * Gordie is a utility class for a buncha lame junk. 
 * Named after "gordo" or "fat", I might have named it
 * Gordito, but hopefully Gordie gets the idea across
 * that it's a fat sack of crap... :-P
 *
 */
class Gordie {

#if flash
	public static function stageWidth() {
		return flash.Lib.current.stage.stageWidth;
	}

	public static function stageHeight() {
		return flash.Lib.current.stage.stageHeight;
	}

	public static function newButton( t:String, x:Int, y:Int, clickFunction:Dynamic->Void ) {
		var upState = Gordie.buttonInnards( t, 0x0000DD );
		var overState = Gordie.buttonInnards( t, 0xAAAAFF );
		var downState = Gordie.buttonInnards( t, 0xFF0000 );
        var button = new SimpleButton( 
			upState, overState, downState, upState
        );
        button.addEventListener( MouseEvent.CLICK, clickFunction );
        button.enabled = true;
		button.x = x;
		button.y = y;
        return button;
	}

	public static function buttonInnards( t:String, color:UInt ) {
		var button = new MovieClip();
		var text = new TextField();
		text.text = t;
		text.x = 0;
		text.y = -6;
		button.addChild( text );
		button.graphics.lineStyle( 2, 0x000020 );
		button.graphics.beginFill( color );
		button.graphics.drawRect( 0, 0, 10, 10 );
		button.graphics.endFill();
		return button;	
	}
#end

	public static function or( a, b ) {
		return null == a ? b : a;
	}

	public static function combinations( stringArrays:Array< Array< String > > ) {
		var results = new Array< String >();
		var n = numberOfCombinations( stringArrays );
		for ( i in 0 ... n ) {
			results.push( '' );
		}
		var down_stream = n;
		for ( stringArray in stringArrays ) {
			down_stream = Math.floor( down_stream / stringArray.length );
			trace( down_stream + ' and ' + down_stream );
			var i = 0;
			while ( i < n ) {
				for ( string in stringArray ) {
					for ( j in 0 ... down_stream ) {
						results[ i++ ] += string;
					}
				}
			}
		}

		return results;
	}

	public static function numberOfCombinations( stringArrays:Array< Array< String > > ) {
		var count = 1;
		for ( stringArray in stringArrays ) {
			count *= stringArray.length;
		}
		return count;
	}
}
